import {api} from './libs/api.js';
let _self;
export const index =  {
	data() {
		return {
			optLock:false,rebackShow:false,showDel:false,showRefund:false,curOrderId:0,retOrderId:0,tabOrderType:0,qrShow:false,listOpen:false,refundShow:false,payShow:false,
			codeShow:false,imgShow:false,deliverShow:false,showDeliver:false,payInfo:{},orderOptShow:false,payAgainShow:false,orderOpt:{},deliverOpt:{},pMoney:0,delOrderId:0,opt:{name:"",id:0},
			rebackInfo:{id:0,msg:""},
			dataList:[],showMaterial:false,materialInfo:{},
			payImgs:[],
			orderStatus:api.orderStatus,
			cerpStatus:api.cerpStatus,
			orderType:api.orderType,
			tabSubItem:1,
			showType:0,
			orderStatusMap:api.orderStatusMap(),
			icons:{"50":uni.upx2px(50),"60":uni.upx2px(60),"45":uni.upx2px(45),"40":uni.upx2px(40),"30":uni.upx2px(30)},
			optInfo:{},
			reload:false,
			payStatus:{},
			rules:{},
			formData:{},
			warehouseList:[],
			warehouseIndex:0,
			qrBoxTitle:""
		}
	},
	
	watch:{
		reload(val){
			if(val){
				this.initData();
			}
		},
		
		materialInfo(val){
			if(val["confirm"]){
				this.dataList[val["index"]]["material_cost"]=val["material_cost"];
				this.dataList[val["index"]]["material_nums"]=val["material_nums"];
				this.dataList[val["index"]]["worker"]=val["worker"];
			}
		},
		
		payInfo(val){
			if(val["pay_mode"]){
				if(val["pay_mode"]==2){

					if(this.payInfo["confirm"]){
						this.setPayStatus();
					}else{
						this.setPayStatus();
						if(val["transfer_type"]=="2"){
							if(val["is_upload"]){
								this.getQrcodeBox();
							}else{
								this.getPayImg();
							}
						}else{
							this.getQrcodeBox();
						}
					}

				}else if(val["pay_mode"]==1){
					this.setPayStatus();
	
				}else if(val["pay_mode"]==3){

					if(this.payInfo["confirm"]){
						this.setPayStatus();
					}else{
						this.getPayCode();
					}
				}
			}
			
		},
		
		orderOpt(val){
			if(val["confirm"]){
				this.submitConfirm();
			}
		}
	},
	
	mounted() {
		_self = this;
	},
	onReachBottom:function(e){
		this.params.page+=1;
		this.initData();
	},
	onLoad:function(option) {
		this.tabSubItem=option.status?option.status:0;
		this.tabOrderType=option.orderType?option.orderType:0;
		this.showType=option.showType?option.showType:0;
	},
	
	methods: {
		async initData(){
			uni.showLoading({title: '加载中'});
			this.optInfo=await api.getOptInfo({});
			
			if(this.warehouseList.length==0){
				this.warehouseList=await api.getWarehouseList({"cPersonCode":this.member["user_code"]});
				if(this.warehouseList&&this.warehouseList.length>0){
					this.formData={
						"warehouse_name":this.warehouseList[0].cWhName,
						"warehouse_code":this.warehouseList[0].cWhCode,
					};
				}				
			}
			
			if(this.optInfo["qr_img"]){
				this.qrBoxTitle=this.optInfo["qr_img"]["box_tite"];
			}
			
			if(this.optInfo["pay_status"]){
				this.payStatus=this.optInfo["pay_status"];
			}
			
			

			this.params["status"]=this.tabSubItem;
			if(this.tabOrderType==4){
				this.params["cerp_sync_need"]=1;
			}else{
				this.params["cerp_sync_need"]=0;
				this.params["pro_type"]=this.tabOrderType;
			}
			this.params["show_type"]=this.showType;
			let orderInfo=await api.dataList(this.params);
			if(this.params.page==1){
				this.dataList=orderInfo["dataList"];
			}else{
				if(orderInfo["dataList"].length==0) this.params.page-=1;
				this.dataList.push.apply(this.dataList,orderInfo["dataList"]);
			}
			this.reload=false;
			this.listOpen=true;
			uni.hideLoading();
		},
		
		selectTab:function(event){
			this.listOpen=false;
			this.params.page=1;
			this.tabSubItem=event.currentTarget.dataset.id;
			this.initData();
		},
		
		
		selectOrderType:function(event){
			this.listOpen=false;
			this.params.page=1;
			this.tabOrderType=event.currentTarget.dataset.id;
			this.initData();
		},
		
		seachInfo(){this.initData();},
		
		
		
		
		submitOrder(index){
			var item= this.dataList[index];

			this.orderOpt={"id":item.id,"pro_type":item.pro_type,"cerp_sync_can":item.cerp_sync_can};
			this.orderOptShow=true;
		},

		async submitConfirm(){

			let retInfo=await api.submitOrder(this.orderOpt);
			if(retInfo=="true"){
				uni.showToast({icon:"none",title:"提交成功！"});
				setTimeout(function(){
					if(_self.orderOpt["pro_type"]=="2") _self.tabSubItem=2.1;	
					_self.initData();
				},2000);　　
			}
		},
		
		
		
		
		
		async auditOrder(id){
			let retInfo=await api.auditOrder({"id":id});
			if(retInfo=="true"){
				uni.showToast({icon:"none",title:"审核成功！"});
				setTimeout(function(){_self.initData();},2000);　　
			}
		},
		
		rebackOrder(id){
			this.rebackShow=true;
			this.rebackInfo.id=id;
		},
		
		editOrder(order_item_id,product_id){
			uni.navigateTo({url:"/pages/product/detail/index?id="+product_id+"&order_item_id="+order_item_id});
		},
		
		getPayCode(){
			let memberInfo =this.$store.state.user.memberInfo
			if(!memberInfo["pay_code"]||memberInfo["pay_code"].length==0){
				uni.showToast({icon:"none",title:"付款码未分配！"});
				return false;
			}
			
			this.codeShow=true;

		},
		
		getPayImg(){
			this.imgShow=true;
		},
		
		
		async confirmReback(){
			let retInfo=await api.rebackOrder(this.rebackInfo);
			if(retInfo=="true"){
				uni.showToast({icon:"none",title:"退回成功！"});
				this.rebackShow=false;
				setTimeout(function(){_self.initData();},2000);　　
			}else{
				this.rebackShow=false;
			}
		},
		
		getQrcodeBox(){
			this.qrShow=true;
		},
		
		async paymentOpt(id,val){
			this.payShow=true;
			let order =await api.dataInfo({id:id});
			if(!order){
				uni.showToast({icon:"none",title:"单据不存在！"});
				return false;
			}
			
			this.payImgs=[];
			if(order["pay_imgs"]) this.payImgs=order["pay_imgs"];

			this.pMoney=(Number(order["total_price"])-Number(order["pay_money"])).toFixed(2);
		
			this.curOrderId=id;
		},
		
		async setPayStatus(){
			let dataInfo={
				"id":this.curOrderId,
				"total_money":this.payInfo["pay_money"],
				"pay_mode":this.payInfo["pay_mode"],
				"pay_type":this.payInfo["pay_type"],
			}
			

			if(this.payInfo["pay_imgs"]&&this.payInfo["pay_imgs"].length>0){
				dataInfo["pay_imgs"]=this.payInfo["pay_imgs"];
			}
			
			let retInfo=await api.setPayStatus(dataInfo);
			
			if(retInfo=="success"&&this.payInfo["pay_mode"]!=2){
				uni.showToast({icon:"none",title:"付款成功！"});
				setTimeout(function(){uni.switchTab({url: "/pages/product/index"});},2000);　　
			}
		},
	
		
		async refundQuery(id){
			this.retOrderId=id;
			this.refundShow=true;
		},
		
		async confirmRefund(id){
			if(this.optLock){
				uni.showToast({icon:"none",title:"请不要重复提交！"});
				return false;
			}
			this.optLock=true;
			
			if(!this.opt["name"]||!this.opt["id"]){
				this.showRefund=false;
				uni.showToast({icon:"none",title:"参数错误！"});
				return false;
			}
			
			
			let retInfo="true";
			switch(this.opt["name"]){
				case "refund":
					retInfo=await api.orderRefund({"id":this.opt["id"]});
					break;
			}
			
			if(retInfo=="true"){
				uni.showToast({icon:"none",title:"退款成功！"});
				this.showRefund=false;
				setTimeout(function(){_self.initData();},2000);　　
			}
			
			this.optLock=false;
			this.showRefund=false;
		},

		async confirmDel(){
			if(!this.opt["name"]||!this.opt["id"]){
				this.showDel=false;
				uni.showToast({icon:"none",title:"参数错误！"});
				return false;
			}
			
			let retInfo="true";
			switch(this.opt["name"]){
				case "delOrder":
					retInfo=await api.deleteOrder({"id":this.opt["id"]});
					break;
				case "delProduct":
					retInfo=await api.delOrderItem({"id":this.opt["id"]});
					break;
			}
			
			if(retInfo=="true"){
				uni.showToast({icon:"none",title:"删除成功！"});
				this.showDel=false;
				setTimeout(function(){_self.initData();},2000);　　
			}
				
		},
		
	    submitDeliver(id){
			this.showDeliver=true;
			this.curOrderId=id;
		},
		
		submitMaterial(index){
			this.materialInfo["index"]=index;
			this.materialInfo["id"]=this.dataList[index].id;
			this.showMaterial=true;
		},
		
		async confirmDeliver(){
			let dataPost=Object.assign({"id":this.curOrderId,"is_deliver":2},this.formData);
			let retInfo = await api.setDeliverStatus(dataPost);
			if(retInfo=="success"){
				uni.showToast({icon:"none",title:"发货成功！"});
				this.showDeliver=false;
				setTimeout(function(){_self.initData();},2000);　　
			}
		},
		
		deliverChange(e){
			let index=e.detail.value;
			this.formData={
				"warehouse_name":this.warehouseList[index].cWhName,
				"warehouse_code":this.warehouseList[index].cWhCode,
			};
			this.warehouseIndex=index;
		},
		
		payAgain(id){
			this.payAgainShow=true;
			this.curOrderId=id;
		},
		
		deliverInfo(id){
			this.deliverOpt={"order_id":id};
			this.deliverShow=true;
		},
		
		async payAgainConfirm(){
			let retInfo = await api.orderClone({"id":this.curOrderId});
			if(retInfo=="success"){
				uni.showToast({icon:"none",title:"下单成功！"});
				this.payAgainShow=false;
				this.tabSubItem="1";
				setTimeout(function(){_self.initData();},2000);　　
			}
		}
		
		
				
	},
}